package com.heo.util;

import java.util.Map;

import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class DeviceNettyUtils {

	private static ScriptEngine engine;
	
	static {
		engine = new ScriptEngineManager().getEngineByName("JavaScript"); 
	}
	
	public static void deviceInitResult(Map<String, Object> rspJsonMap) {
		rspJsonMap.put("t", 150); // 心跳时间
		rspJsonMap.put("n", 3); // 心跳次数重启
		rspJsonMap.put("v", 4); // 获取指令v*10秒没有检测到启动就重启
		rspJsonMap.put("y", 2); // y // 数据类型：数值，长度小于7字符，描述：连续投币延时时间，单位=秒，分辨率1秒，默认10秒
		rspJsonMap.put("i", 30); // 心跳超时时间
	}
	
	/**
	 * 字符集;分割  获取其中某个
	 * @param scriptEngineCmd
	 * @param size
	 * @return
	 */
	public static String getArrayString(String scriptEngineCmd, int size) {
		if(Utils.checkNull(scriptEngineCmd)) {
			String[] scriptEngineCmds = scriptEngineCmd.split(";");
			if(scriptEngineCmds.length >= (size + 1)) {
				return scriptEngineCmds[size];
			}
		}
		return "";
	}
	
	public static String scriptEngineCmd(String exec, String cmd) throws Exception {
		Compilable compilable = (Compilable) engine;  
		Bindings bindings = engine.createBindings(); // Local级别的Binding  
		CompiledScript jSFunction = compilable.compile(exec); // 解析编译脚本函数  
        bindings.put("cmd", cmd);  
        Object resultJs = jSFunction.eval(bindings);  
        return Utils.getStr(resultJs);
	}
}
